<?php
/*
 * Descripción: Carga todas las preguntas según el filtro indicado
 */
if($_REQUEST["q"] == "1"){
    //Variables del jqgrid
    $page = $_GET['page']; // get the requested page
    $limit = $_GET['rows']; // get how many rows we want to have into the grid
    $sidx = $_GET['sidx']; // get index row - i.e. user click to sort
    $sord = $_GET['sord']; // get the direction
    if(!$sidx) $sidx =1;
    
    if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
        header("Content-type: application/xhtml+xml;charset=utf-8");
    } else {
        header("Content-type: text/xml;charset=utf-8");
    }
    $et = ">";
    echo "<?xml version='1.0' encoding='utf-8'?$et\n";
    echo "<rows>";

    require_once '../funciones/funciones.php';   //Se hace la inclusion para que no genere conflictos con el XML
    $con = new MySQL();
    $where = ($_REQUEST['tipo'] != "")?" and preg.pregTipo = '{$_REQUEST['tipo']}'":"";
    $where .= ($_REQUEST['tema'] != "")?" and preg.temaId = '{$_REQUEST['tema']}'":"";
    $where .= ($_REQUEST['nivel'] != "")?" and preg.niveId = '{$_REQUEST['nivel']}'":"";
    $where .= ($_REQUEST['subtema'] != "")?" and preg.subtemaId = '{$_REQUEST['subtema']}'":"";
    $where .= ($_REQUEST['contiene'] != "")?" and preg.pregDes like '%{$_REQUEST['contiene']}%'":"";
    $sql = "select COUNT(pregId) as count from preguntas preg 
            inner join temas tem on tem.temaId = preg.temaId
            inner join subtemas sub on sub.subtemaId = preg.subtemaId
            inner join niveles niv on niv.niveId = preg.niveId where preg.pregEstado = 'A' $where";
    $result = $con->consulta($sql);
    $row = mysql_fetch_array($result,MYSQL_ASSOC);
    $count = $row['count'];

    if( $count >0 ) $total_pages = ceil($count/$limit);
    else $total_pages = 0;
    if ($page > $total_pages) $page=$total_pages;
    $start = $limit*$page - $limit; // do not put $limit*($page - 1)
    $start = ($start < 0)?0:$start;
    $sql = "select tem.temaNombre,sub.subtemaNombre,niv.nivel,preg.pregDes,preg.pregId,preg.pregTipo from preguntas preg 
            inner join temas tem on tem.temaId = preg.temaId
            inner join subtemas sub on sub.subtemaId = preg.subtemaId
            inner join niveles niv on niv.niveId = preg.niveId where preg.pregEstado = 'A' $where ORDER BY $sidx $sord LIMIT $start , $limit";
    $result = $con->consulta($sql);
    
    echo "<page>".$page."</page>";
    echo "<total>".$total_pages."</total>";
    echo "<records>".$count."</records>"; // be sure to put text data in CDATA$sql
    //Consulta cantidad
    while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
        echo "<row id='".$row['pregId']."'>";
        echo "<cell></cell>";
        $tmp = ($row['pregTipo'] == "T")?"Teórica":"Práctica";
        echo "<cell>".$tmp."</cell>";
        echo "<cell>".htmlspecialchars($row['temaNombre'])."</cell>";
        echo "<cell>".htmlspecialchars($row['subtemaNombre'])."</cell>";
        echo "<cell>".htmlspecialchars($row['nivel'])."</cell>";
        echo "<cell>".htmlspecialchars($row['pregDes'])."</cell>";
        
        echo "</row>";
    }
    echo "</rows>";
}
 elseif ($_REQUEST["q"] == "2") {
    $pregId = $_REQUEST["id"];
    if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
        header("Content-type: application/xhtml+xml;charset=utf-8");
    } else {
        header("Content-type: text/xml;charset=utf-8");
    }
    $et = ">";
    echo "<?xml version='1.0' encoding='utf-8'?$et\n";
    echo "<rows>";
    require_once '../funciones/funciones.php';   //Se hace la inclusion para que no genere conflictos con el XML
    $con = new MySQL();
    $sql = "select * from preguntas where pregId = '$pregId'";
    $result = $con->consulta($sql);
    $preguntas = $con->unaFila($result);
    if(strtoupper($preguntas["pregTipo"]) == "P"){
        echo "<row>"; 
        echo "<cell></cell><cell></cell>"; 
        echo "<cell>".htmlspecialchars($preguntas['pregSolucion'])."</cell>"; 
        $temp = $preguntas['pregValor1'].",".$preguntas['pregValor2'].",".$preguntas['pregValor3'].",".$preguntas['pregValor4'];
        echo "<cell>". htmlspecialchars($temp)."</cell>"; 
        echo "<cell>". htmlspecialchars($preguntas['pregResultado'])."</cell>"; 
        echo "</row>";
    }
    elseif(strtoupper($preguntas["pregTipo"]) == "T"){
        $sql = "select * from distractores where pregId = '$pregId' order by distraOrden";
        $result =$con->consulta($sql);
        while($row = $con->unaFila($result)){
            echo "<row>"; 
            echo "<cell>".$row["distraOrden"]."</cell>";
            $temp = ($row["distraClave"] == "1")?"Si":"No";
            echo "<cell>".$temp."</cell>"; 
            echo "<cell>".htmlspecialchars($row['distraDescripcion'])."</cell>"; 
            echo "<cell>No aplica</cell><cell>No aplica</cell>"; 
            echo "</row>";
        }
    }
    echo "</rows>";
}
?>
